{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7d7ecad0-1097-4f0f-91da-2ee09b3d1135",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-05T20:34:33.781805Z",
     "iopub.status.busy": "2022-02-05T20:34:33.781511Z",
     "iopub.status.idle": "2022-02-05T20:34:33.879001Z",
     "shell.execute_reply": "2022-02-05T20:34:33.878393Z",
     "shell.execute_reply.started": "2022-02-05T20:34:33.781738Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1a25b14-97ca-4496-9fb5-13c8dced06f2",
   "metadata": {},
   "source": [
    "1. 使用直接索引和切片来替换\n",
    "2. 使用布尔索引来替换\n",
    "3. 使用where函数来替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2e954c7b-57e7-4919-b8db-64ebcc1859cf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-05T20:36:01.844483Z",
     "iopub.status.busy": "2022-02-05T20:36:01.844245Z",
     "iopub.status.idle": "2022-02-05T20:36:01.850013Z",
     "shell.execute_reply": "2022-02-05T20:36:01.849174Z",
     "shell.execute_reply.started": "2022-02-05T20:36:01.844459Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 7 2 6 6]\n",
      " [1 7 3 8 4]\n",
      " [6 5 6 0 5]]\n"
     ]
    }
   ],
   "source": [
    "a1=np.random.randint(0,# 使用布尔索引进行元素替换\n",
    "a1[a1<5] = 3\n",
    "print(a1)10,size=(3,5)) # 构造3行5列的数组，元素为0到9之 间的随机整数\n",
    "print(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b4cf45e7-4ad2-43a6-aeca-0dd820eab888",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-05T20:38:30.270168Z",
     "iopub.status.busy": "2022-02-05T20:38:30.269814Z",
     "iopub.status.idle": "2022-02-05T20:38:30.274106Z",
     "shell.execute_reply": "2022-02-05T20:38:30.273399Z",
     "shell.execute_reply.started": "2022-02-05T20:38:30.270091Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 7 2 6 6]\n",
      " [0 0 0 0 0]\n",
      " [6 5 6 0 5]]\n"
     ]
    }
   ],
   "source": [
    "# 通过直接索引进行元素替换\n",
    "a1[1]=0 # 把第1列所有元素都替换成0\n",
    "print(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "df8cf61c-3c84-4bd4-8ca2-71dc6cd3ac4e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-05T20:38:59.370162Z",
     "iopub.status.busy": "2022-02-05T20:38:59.369903Z",
     "iopub.status.idle": "2022-02-05T20:38:59.374554Z",
     "shell.execute_reply": "2022-02-05T20:38:59.373752Z",
     "shell.execute_reply.started": "2022-02-05T20:38:59.370136Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 7 2 6 6]\n",
      " [1 2 3 4 5]\n",
      " [6 5 6 0 5]]\n"
     ]
    }
   ],
   "source": [
    "a1[1]=np.array([1,2,3,4,5]) 把第1列换成[1 2 3 4 5]\n",
    "print(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "761a42b2-dac4-45ee-b42c-9c95fc60e9e7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-05T20:41:46.710175Z",
     "iopub.status.busy": "2022-02-05T20:41:46.709912Z",
     "iopub.status.idle": "2022-02-05T20:41:46.715731Z",
     "shell.execute_reply": "2022-02-05T20:41:46.714383Z",
     "shell.execute_reply.started": "2022-02-05T20:41:46.710149Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 7 3 6 6]\n",
      " [3 3 3 3 5]\n",
      " [6 5 6 3 5]]\n"
     ]
    }
   ],
   "source": [
    "# 使用布尔索引进行元素替换\n",
    "a1[a1<5] = 3\n",
    "print(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "39c20b13-e4ae-42ac-8b26-dc58e2038367",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-05T20:45:55.539029Z",
     "iopub.status.busy": "2022-02-05T20:45:55.538784Z",
     "iopub.status.idle": "2022-02-05T20:45:55.543960Z",
     "shell.execute_reply": "2022-02-05T20:45:55.542965Z",
     "shell.execute_reply.started": "2022-02-05T20:45:55.539005Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 0 1 1]\n",
      " [0 0 0 0 1]\n",
      " [1 1 1 0 1]]\n"
     ]
    }
   ],
   "source": [
    "# 使用where函数进行元素替换\n",
    "print(np.where(a1<5,0,1)) # 指定判断条件为小于5，如果小于5就替换0，否则替换为1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
